<template>
  <div class="page-list-live">
    <PageList>
      <template #header>账号管理</template>
      <template #filter>
        <ListFilter :form="topFilter" :key-list="keyList" @search="handleSearch" @reset="handleReset"></ListFilter>
      </template>
      <template #tips>
        <el-button type="primary" icon="el-icon-document-add" @click="handleAdd">新增账号</el-button>
      </template>
      <template #table>
        <ListTable ref="RefListTable" :loading="tableLoading" :table-data="tableData" :table-col="tableCol">
          <template slot="col1" slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              :loading="scope.row.loadingEdit"
              size="mini"
              type="text"
              style="margin-left: 0px; margin-right: 10px;"
              @click="handleRowEdit(scope.row)"
            >编辑</el-button>
            <el-button
              :loading="scope.row.loadingDel"
              :disabled="scope.row.loginName === 'admin'"
              size="mini"
              type="text"
              icon="el-icon-delete"
              style="color: red; margin-left: 0px; margin-right: 10px;"
              @click="handleRowDel(scope.row)"
            >删除</el-button>
          </template>
        </ListTable>
      </template>
    </PageList>
    <AccountsEdit ref="RefAccountsEdit" @success="afterEditSuccess"></AccountsEdit>
  </div>
</template>

<script>
  import listMixin from '@/mixins/list'
  import AccountsEdit from './components/AccountsEdit.vue'
  import ListFilter from '@/components/ListFilter'
  import { user } from '@/api/api'
  import { mapGetters } from 'vuex'
  export default {
    components: {
      AccountsEdit,
      ListFilter
    },
    mixins: [listMixin],
    data() {
      const _this = this

      return {
        topFilter: {
          keyword: { label: '关键字', placeholder: '请输入用户名或角色' }
        },
        keyList: ['name', 'loginName'],
        tableData: [],
        tableCol: [
          { type: 'index', label: '序号', width: '60px' },
          { prop: 'name', label: '显示名称' },
          { prop: 'loginName', label: '用户名' },
          {
            prop: 'isAdmin',
            label: '角色',
            formatter(row, col, cell) {
              return cell ? '管理员' : '普通用户'
            }
          },
          {
            prop: 'createdTime',
            label: '创建时间',
            formatter(row, col, cell) {
              return _this.$format(row.createdTime, true)
            }
          },
          {
            prop: 'isEnable',
            label: '状态',
            formatter(row, col, cell) {
              return cell ? '禁用' : '启用'
            }
          },
          { label: '操作', custom: 'col1' }
        ]
      }
    },
    computed: {
      ...mapGetters(['name'])
    },
    methods: {
      // 请求列表数据
      async getList(query) {
        this.tableLoading = true
        try {
          let obj = query || { pageIndex: 1, pageSize: 10, orderBy: 'createdTime', orderByAscent: false }
          const {
            data: { items: resData, count, pageIndex }
          } = await user('post', obj, false)
          this.tableData = []
          this.tableData = resData.map(item => {
            item.loadingEdit = false
            item.loadingDel = false
            return item
          })
          this.$nextTick(() => {
            this.$refs['RefListTable'].update({ page: pageIndex, count })
            this.tableLoading = false
          })
        } catch (err) {
          this.tableLoading = false
          this.tableData = []
        }
      },
      async afterEditSuccess() {
        this.getList(this.serchObj)
      },
      handleAdd() {
        let objData = {
          // 进入哪一个程序
          status: 'add',
          apiKey: {
            add: '/User', // 添加
          }
        }
        this.$refs['RefAccountsEdit'].openOne(objData)
      },
      handleRowEdit(row) {
        let objData = {
          // 进入哪一个程序
          status: 'edit',
          // 源数据
          row: row,
          // 源id
          id: row.id,
          // 查询参数
          searchData: {
            url: '/User/' + row.id,
            method: 'GET',
            params: null
          }
        }
        this.$refs['RefAccountsEdit'].openOne(objData)
      },
      async delRowReq(row) {
        try {
          let obj = { id: row.id, value: null }
          this.getFlag(row.loginName, this.name, async (flag) => {
            console.log(flag)
            await user('delete', obj, true)
            row.loadingDel = false
            this.getList(this.serchObj)
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            if (!flag) {
              return
            }
            await this.$store.dispatch('user/logout')
            this.$router.push(`/login?redirect=${this.$route.fullPath}`)
          })
          
          
        } catch (err) {
          row.loadingDel = false
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        }
      },
      handleRowDel(row) {
        row.loadingDel = true
        this.$_delConfirm(
          '该账号',
          () => {
            this.delRowReq(row)
          },
          () => {
            row.loadingDel = false
          }
        )
      },
      getFlag(value, data, fn) {
        console.log(value, data)
        let flag = false
        if (value === data) {
          flag = true
        } else {
          flag = false
        }
        fn(flag)
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
